<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个人中心</title>
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/mycenter.css">
    <script src="js/commom.js"></script>
    <script src="js/jquery.min.js"></script>
</head>

<body>
   <!-- 导航栏 -->
   <div class="nav">
    <span class="nav_left">
        <img src="img/logo.jpg" alt="">
    </span>
    <span class="nav_right">
        <a href="topic_list.html">主页</a>
        <!-- <a href="blog_edit.html">写博客</a> -->
        <a href="reg.html">注册</a>
        <!-- <a href="#">注销</a> -->
    </span>  
</div>
    <!-- 提示框 -->
    <div id="pr">

    </div>
    <!-- 版心 -->
    <div class="login-container">
        <!-- 中间的登陆框 -->
        <div class="login-dialog">
            <h2>个人信息</h2>
                <!-- 头像 -->
                <div class="row" >
                    <span class="ph">头像：</span>
                    <img src="img/head.png" alt="头像" height="30" id="photo">
                    <input type="file" style="text-indent:-9999px; margin-left: -200px;" name="file">
                </div>
                <div class="row">
                    <span>性别：</span>
                    <input type="radio" class="gender" name="gender" value="男" id="man" >男
                    <input type="radio" class="gender" name="gender" value="女" id="woman">女
                </div>
                <div class="row">
                    <span>用户昵称：</span>
                    <input type="text" id="nickname">
                </div>
            
           
                <div class="row">
                    <span>电子邮箱：</span>
                    <input type="text" id="email">
                </div>
                <div class="row">
                    <span>联系方式：</span>
                    <input type="text" id="phone">
                </div>
            
                <div class="row">
                    <span>修改密码: </span>
                
                </div>
                <div class="row">
                    <span>旧密码: </span>
                    <input type="password" id="password" maxlength="10">
                </div>
                <div class="row">
               
                    <span>新密码: </span>
                    <input type="password" id="password1" maxlength="10">
                </div>
                <div class="row">
               
                    <span>确认密码: </span>
                    <input type="password" id="password2" maxlength="10">
                </div>
                <div class="row">
                    <span>私密信息: </span>
                
                </div>
                <div class="row">
                    <span>你喜欢什么？: </span>
                    <input type="text" id="lover">
                </div>
                
                <div class="row">
                    <button id="submit" onclick="upmyinfo()">保存</button>
                </div>
                
        </div>
    </div>
    <script>
        function upmyinfo() {
            var gender = jQuery("input:radio:checked").val();
            var nickname = jQuery("#nickname");
            var email = jQuery("#email");
            var phone = jQuery("#phone");
            var password = jQuery("#password");
            var password1 = jQuery("#password1");
            var password2 = jQuery("#password2");
            var lover = jQuery("#lover");
            //如果修改密码，需要后两次密码一致,首先需要输入旧密码
            if(password.val() == "" && password1.val()!="") {
                alert("如需修改密码，请先输入旧密码");
                password.focus();
                return;
            }
            if(password.length > 10) {
                alert("密码格式有误");
                password.focus();
                return;
            }
            if(password.val() != "" && password1.val() == "") {
                alert("请输入新密码");
                password1.focus();
                return;
            }
            if(password1.length > 10) {
                alert("密码格式有误");
                password1.focus();
                return;
            }
            if(password.val() != "" && password2.val() == "") {
                alert("请确认新密码");
                password2.focus();
                return;
            }
            if(password2.length > 10) {
                alert("密码格式有误");
                password2.focus();
                return;
            }
            if(password1.val() != password2.val()) {
                alert("确认密码不一致");
                password1.focus();
                return;
            }
            //新旧密码一致
            if(password.val() != "" && password.val() == password1.val()) {
                alert("新旧密码一致，请重新输入~");
                password1.focus();
                return;
            }
            //无信息添加时，无需发送请求
            if(nickname.val() == "" && email.val() == "" && phone.val() == "" && password.val() == "" && lover.val() == "") {
                if(!confirm("无信息更新，无需保存,是否继续编辑")) {
                    location.href = "/blog/myblog_list.html";
                    
                } 
                return;
            } 
            //向后端发送数据
            jQuery.ajax({
                url: "/blog/user/upuserinfo",
                type: "POST",                                            ////新密码                                                                      //旧密码                      
                data:{"gender":gender,"nickname":nickname.val(),"password":password1.val(),"email":email.val(),"phone":phone.val(),"lover":lover.val(), "oldpassword":password.val()},
                success:function(result) {
                    if(result != null && result.code == 200 && result.data == 1) {
                        alert("修改成功！");
                        location.href = "/blog/myblog_list.html";
                        return;
                    } else {
                        alert("抱歉，修改失败");
                        return;
                    }
                }
            });
        }
        //加载已有的个人信息
        function showcenter() {
            jQuery.ajax({
                url :"/blog/user/showcenter",
                type:"POST",
                data:{},
                success:function(result) {
                    if(result != null && result.code == 200){
                        if(result.data.gender != null) {
                            if(result.data.gender == '女') {
                                $('input:radio:last').attr('checked', 'checked');
                                // jQuery("input[name='gender'][value='woman']").prop("checked",true);
                            } else {
                                $('input:radio:first').attr('checked', 'checked');
                                // jQuery("input[name='gender'][value='man']").prop("checked",true);
                            }
                             
                            // jQuery("input:radio:checked").val(result.data.gender);
                        }
                        if(result.data.nickname != null){
                            jQuery("#nickname").val(result.data.nickname);
                        }
                        if(result.data.email != null){
                            jQuery("#email").val(result.data.email);
                        }
                        if(result.data.phone != null) {
                            jQuery("#phone").val(result.data.phone);
                        }
                        if(result.data.lover != null) {
                            jQuery("#lover").val(result.data.lover);
                        }
                    }
                }
            });
        }
        showcenter();
    </script>
</body>

</html>